$(function () {
    //提交按钮点击事件
    $("#submitBtn").click(function () {
        //获得文本框的值
        var exchangeName=$("#exchangeName").val();
        var stampDuty=$("#stampDuty").val();
        var transferRate=$("#transferRate").val();
        var exchangeId=$("#exchangeId").val();
        var handlingFeeRate=$("#handlingFeeRate").val();
        var collectionRate=$("#collectionRate").val();
        var description=$("#description").val();
        // console.log(custodianid.length==0)
        //判断是否存在Id,有Id调用修改的方法，没Id调用添加
        if (exchangeId.length==0){//添加
            $.ajax({
                url:"exchange/insertExchange",
                type:"post",
                data:{"exchangeName":exchangeName,"stampDuty":stampDuty,"transferRate":transferRate,"handlingFeeRate":handlingFeeRate,"collectionRate":collectionRate,"description":description},
                success:function (data) {
                    layer.msg(data);
                    window.location.reload();
                }
            })
        }else{//修改
            $.ajax({
                url:"exchange/updateExchange",
                type:"post",
                data:{"exchangeName":exchangeName,"stampDuty":stampDuty,"transferRate":transferRate,"handlingFeeRate":handlingFeeRate,"collectionRate":collectionRate,"exchangeId":exchangeId,"description":description},
                success:function (data) {
                    layer.msg(data);
                    //刷新页面
                    window.location.reload();
                }
            })
        }
    });
    /*-------------表字段数据开始----------------*/
    layui.use('table', function() {
        var table = layui.table;
        table.render({
            elem: '#test'
            , url: '/exchange/exchangeList'//控制器中条件查询的分页列表
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print']
            , title: '交易品种费率表'
            , skin: 'row' //列边框风格
            , even: true //开启隔行背景
            , size: 'lg' //大尺寸的表格
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {type: 'numbers', title: '序号', width: '80'}
                , {field: 'exchangeId', title: 'ID', width: 180, unresize: true, hide: true}
                , {field: 'exchangeName', title: '交易品种', width: 200}
                , {field: 'stampDuty', title: '印花税', width: 200}
                , {field: 'transferRate', title: '过户费', width: 200}
                , {field: 'handlingFeeRate', title: '经手费', width: 200}
                , {field: 'collectionRate', title: '征管费', width: 200}
                , {field: 'description', title: '描述', width: 600, edit: 'text'}

            ]]
            ,done : function(res, curr, count) {//代表表头，通过设置它的样式
                tableList = res.data;
                $('th').css({'background-color': '#2BB3D5', 'color': '#fff', 'font-weight': 'bold'})
            }
            //开启分页
            , page: true
            , id: "dataTable"
        });
        /*------------------表字段结束---------------*/



        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var layer = layui.layer;
            switch(obj.event){
                //编辑框
                case 'getCheckData':
                    var data = checkStatus.data;
                    if(data.length >1){
                        layer.msg("一次只能修改一个");
                        break;
                    }else if(data.length< 1){
                        layer.msg("请选择一条需要修改的数据");
                        break;
                    }else{
                        layer.open({
                            type:1,
                            title:'修改交易品种费率',
                            offset:'80px',
                            move:false,
                            content:$("#saveOrUpdateDiv"),
                            area:['500px','500px'],
                        })
                        $("#exchangeId").val(data[0].exchangeId);
                        $("#exchangeName").val(data[0].exchangeName);
                        $("#stampDuty").val(data[0].stampDuty);
                        $("#transferRate").val(data[0].transferRate);
                        $("#handlingFeeRate").val(data[0].handlingFeeRate);
                        $("#collectionRate").val(data[0].collectionRate);
                        $("#description").val(data[0].description);
                        break;
                    }
                //console.log(data[0])
                //添加页面
                case 'add':
                    var data = checkStatus.data;
                    layer.open({
                        type:1,
                        title:'添加交易id',
                        offset:'80px',
                        move:false,
                        content:$("#saveOrUpdateDiv"),
                        area:['800px','500px'],
                    });
                    //清空文本框的值
                    $("#exchangeId").val("");
                    $("#exchangeName").val("");
                    $("#stampDuty").val("");
                    $("#transferRate").val("");
                    $("#handlingFeeRate").val("");
                    $("#collectionRate").val("");
                    $("#description").val("");
                    break;

                //删除
                case 'delete':
                    //获得复选框所选数组
                    var check =table.checkStatus("dataTable"),data=checkStatus.data;
                    if(data==""){
                        layer.msg("至少选择一行进行操作！！！",{icon:2});
                        return;
                        break;
                    }
                    //被选中的
                    var ids ="";
                    if(data.length >0){
                        for(var i=0; i<data.length; i++){
                            ids +=data[i].exchangeId+",";
                            console.log(ids);
                        }
                    }
                    layer.confirm("确定要删除当前选中的数据吗?"+ids,function (index) {
                        $.ajax({
                            type:'post',
                            url:'exchange/deleteExchange',
                            data:{"ids":ids.substr(0,ids.length-1)},
                            success:function(data){
                                layer.msg("删除成功",{icon:1});
                                // $(".layui-form-checked").not(".header").parents("tr").remove();
                                window.location.reload();
                            },
                            error:function(data){
                                alert("失败");
                            }
                        });
                    });
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });
        /*-------------------头工具栏事件结束---------------------*/

        //模糊查询
        $("#seekBtn").click(function () {
            //获得输入框的值
            var d=$("#demoReload").val()
            table.reload("dataTable", {
                url: "/exchange/selectExchangeLike"
                , where: {
                    condition:d
                }
            });
        });


    });



});
